package org.jks.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jks.config.Config;
import org.jks.model.DeviceAcceptance;
import org.jks.request.model.Ids;
import org.jks.request.model.UIDeviceAcceptance;
import org.jks.response.model.ActionResult;
import org.jks.response.model.UITable;
import org.jks.service.DeviceAcceptanceService;
import org.jks.service.DeviceService;
import org.jks.util.DateUtil;
import org.jks.util.JsonUtil;
import org.jks.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/device_acceptance")
public class DeviceAcceptanceController {
	@Autowired
	DeviceAcceptanceService deviceAcceptanceService;
	
	@Autowired
	DeviceService deviceService;
	
	@RequestMapping("/add")
	public void add(UIDeviceAcceptance uiDeviceAcceptance, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		ActionResult ar = new ActionResult();
		try {
			DeviceAcceptance da = new DeviceAcceptance();
			da.setDevice(deviceService.findById(uiDeviceAcceptance.getDevice()));
			da.setAddTime(DateUtil.toDate(uiDeviceAcceptance.getTime(), "yyyy-MM-dd HH:mm:ss"));
			da.setReason(uiDeviceAcceptance.getReason());
			da.setState(uiDeviceAcceptance.getState());
			da.setId(uiDeviceAcceptance.getId());
			if(da.getId() != 0)
			{
				deviceAcceptanceService.update(da);
			}
			else
			{
				deviceAcceptanceService.save(da);
			}
			
			ar.setSuccess(true);
		} catch (Exception e) {
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		
		ResponseUtil.returnJson(response, ar);
	}
	
	
	@RequestMapping("/delete")
	public void delete(Ids ids, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			deviceAcceptanceService.delete(ids.getIds());
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete/{id}")
	public void delete(@PathVariable long id, HttpServletRequest request, HttpServletResponse response){
//		ActionResult ar = new ActionResult();
//		try{
//			deviceInOutService.delete(id);
//			ar.setSuccess(true);
//		}
//		catch(Exception e){
//			ar.setMsg(e.getMessage());
//			e.printStackTrace();
//		}
//		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/all")
	public void all(HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		String[] limit = request.getParameterValues("limit");
		String[] pages = request.getParameterValues("page");
		int pagesize = 25;
		int page = 1;
		if(limit.length > 0){
			pagesize = Integer.valueOf(limit[0]);
		}
		if(pages.length > 0){
			page = Integer.valueOf(pages[0]);
		}
		try{
			long total = deviceAcceptanceService.count();
			List<DeviceAcceptance> deviceAcceptances = deviceAcceptanceService.list(page, pagesize);
			if(deviceAcceptances != null && deviceAcceptances.size() != 0)
			{
				for(DeviceAcceptance d : deviceAcceptances)
				{
					d.getDevice().setDeviceRe(null);
					d.getDevice().setFlight(null);
					d.getDevice().setWork(null);
				}
			}
			ar.setSuccess(true);
			UITable table = new UITable();
			table.setTotal(total);
			table.setRows(deviceAcceptances);
			ar.setData(table);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/detail/{id}")
	public ModelAndView detail(@PathVariable long id, HttpServletRequest request, HttpSession session, HttpServletResponse response, Map<String,Object> model){
		try {
			DeviceAcceptance da = deviceAcceptanceService.findById(id);
			da.getDevice().setDeviceRe(null);
			da.getDevice().setFlight(null);
			da.getDevice().setWork(null);
			model.put("DeviceAcceptance", JsonUtil.Object2Json(da));
		} catch (Exception e) {
			e.printStackTrace();
		}
		Config.init(request, response, session, model);
		return new ModelAndView("wuziMgr/device_acceptance_detail", model);
	}
}
